﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Data.SqlClient;
using DevExpress.XtraEditors;
using prjBULHRM;
using System.IO;
namespace prjQuanLyNhanSu
{
    public partial class frmNhanVien : DevExpress.XtraEditors.XtraForm
    {
        public frmNhanVien()
        {
            InitializeComponent();
        }
        public bool Sua { get; set; }
        frmLuoiNhanVien frmLuoi = new frmLuoiNhanVien();

        clsBULHRNhanVien cls = new clsBULHRNhanVien();
        clsBULQT_NguoiPhuThuoc clsQuanHe = new clsBULQT_NguoiPhuThuoc();
        clsBULTT_QuocTich clsQuocTich = new clsBULTT_QuocTich();
        clsBULTT_DanToc clsDanToc = new clsBULTT_DanToc();
        clsBULTT_TonGiao clsTonGiao = new clsBULTT_TonGiao();
        clsBULTT_PhongBan clsPhongBan = new clsBULTT_PhongBan();
        clsBULTT_ChucVu clsChucVu = new clsBULTT_ChucVu();
        clsBULHT_Ca clsCa = new clsBULHT_Ca();
        clsBULTT_CongViec clsCongViec = new clsBULTT_CongViec();
        clsBULTT_LoaiNhanVien clsLoaiNV = new clsBULTT_LoaiNhanVien();
        clsBULHT_QuanHe clsQHe = new clsBULHT_QuanHe();
        clsBULQT_NguoiPhuThuoc clsQTNguoiPhuThuoc = new clsBULQT_NguoiPhuThuoc();

        private void KTDuLieu()
        {
            bool chuan = false;
            if (txtHoTenNV.Text == "")
            {
                MessageBox.Show("Không được rỗng !");
            }
        }
        private void LoadDuLieu()
        {
            dgrNhanVien.DataSource = clsQTNguoiPhuThuoc.SelectAll();
            cboCa.DataSource = clsCa.SelectAll();
            cboCa.DisplayMember = "TenCa";
            cboCa.ValueMember = "CaID";

            cboChucVu.DataSource = clsChucVu.SelectAll();
            cboChucVu.DisplayMember = "Name";
            cboChucVu.ValueMember = "ChucVu_ID";

            cboCongViec.DataSource = clsCongViec.SelectAll();
            cboCongViec.DisplayMember = "Name";
            cboCongViec.ValueMember = "CongViec_ID";

            cboLoaiNV.DataSource = clsLoaiNV.SelectAll();
            cboLoaiNV.DisplayMember = "Name";
            cboLoaiNV.ValueMember = "NhanVien_ID";

            cboLoaiQuanHe.DataSource = clsQHe.SelectAll();
            cboLoaiQuanHe.DisplayMember = "TenQuanHe";
            cboLoaiQuanHe.ValueMember = "QuanHeID";

            cboPhongBan.DataSource = clsPhongBan.SelectAll();
            cboPhongBan.DisplayMember = "Name";
            cboPhongBan.ValueMember = "PhongBan_ID";

            cboQuocTich.DataSource = clsQuocTich.SelectAll();
            cboQuocTich.DisplayMember = "Name";
            cboQuocTich.ValueMember = "QuocTich_ID";

            cboTonGiao.DataSource = clsTonGiao.SelectAll();
            cboTonGiao.DisplayMember = "Name";
            cboTonGiao.ValueMember = "TonGiao_ID";

            cboDanToc.DataSource = clsDanToc.SelectAll();
            cboDanToc.DisplayMember = "Name";
            cboDanToc.ValueMember = "DanToc_ID";



        }

        private void frmNhanVien_Load(object sender, EventArgs e)
        {
            LoadDuLieu();
            if (Sua != false)//sua
            {
                //khoa text manv 
                txtMaNV.Enabled = false;
            }
        }

        private void HinhNV_Click(object sender, EventArgs e)
        {
            OpenFileDialog open = new OpenFileDialog();
            open.Filter = "JPEG *.jgp| *.jpg|BitMap *.bmp| *.bmp";//Loc file hinh
            open.Multiselect = false;
            open.ShowDialog();
            if (open.FileName != "")
            {
                this.HinhNV.ImageLocation = open.FileName;
            }
        }

        private void btnLuu_CheckedChanged(object sender, EventArgs e)
        {           
            string strFile = "";
            //if (dNgaySinhNV.DateTime == null || txtNoiSinh.Text.Trim() == "" || cboTinhTrangHonNhan.SelectedIndex.ToString() == "" ||
            //    txtLuongCoBan.Text== "" || txtHeSoLuong.Text.Trim() == "" || dNgayVaoLam.DateTime == null || txtSoNgayPhep.Text.Trim() == "")
            //{
            //    MessageBox.Show("Bạn nhập thông tin chưa đầy đủ. Hãy xem lại !", "Thông Báo",MessageBoxButtons.OK,MessageBoxIcon.Warning);
            //}
            // else
            //{
            if (txtMaNV.Text == "" || txtHoTenNV.Text == "")
            {
                MessageBox.Show("Bạn chưa nhập mã nhân viên hoặc họ tên nhân viên !", "Thông Báo", MessageBoxButtons.OK, MessageBoxIcon.Warning);
            }
            else
            {
                if (dNgaySinhNV.DateTime == DateTime.Now || dNgayKTHC.DateTime == DateTime.Now || dNgaySinhNT.DateTime == DateTime.Now || dNgaySinhNV.DateTime == DateTime.Now || dNgayVaoLam.DateTime == DateTime.Now || dNgayCapSTDoan.DateTime == DateTime.Now || dNgayCapSTDang.DateTime == DateTime.Now || dNgayCapSLD.DateTime == DateTime.Now ||
                dNgayCapCMND.DateTime == DateTime.Now || dNgayCapBHYT.DateTime == DateTime.Now || dNgayCapBHXH.DateTime == DateTime.Now || dNgayBDHC.DateTime == DateTime.Now)
                {
                    MessageBox.Show("Chưa nhập ngày đầy đủ. Hãy kiểm tra lại !", "Thông Báo", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                }
                else
                {
                    cls.NhanVien_ID = txtMaNV.Text;
                    cls.Name = txtHoTenNV.Text;
                    cls.Birth = dNgaySinhNV.DateTime;
                    cls.BirthAdress = txtNoiSinh.Text;
                    cls.Adress = txtThuongTru.Text;
                    cls.T_Adress = txtTramTru.Text;
                    cls.CMND = txtCMND.Text;
                    cls.CMNDDay = dNgayCapCMND.DateTime;
                    cls.CMNDAdress = txtNoiCapCMND.Text;
                    cls.Sex = chkNu.Checked;
                    cls.BiDanh = txtBiDanh.Text;
                    cls.Maried = int.Parse(cboTinhTrangHonNhan.SelectedIndex.ToString());
                    cls.CellPhone = txtDiDongNV.Text;
                    cls.Phone = txtDTNhaNV.Text;
                    cls.Email = txtEmail.Text;
                    cls.MaMay = txtMaMay1.Text + "-" + txtMaMay2.Text;
                    cls.QuocTich = cboQuocTich.SelectedValue.ToString();
                    cls.DanToc = cboDanToc.SelectedValue.ToString();
                    cls.TonGiao = cboTonGiao.SelectedValue.ToString();
                    cls.LoaiNhanVien = cboLoaiNV.SelectedValue.ToString();
                    cls.PhongBan = cboPhongBan.SelectedValue.ToString();
                    cls.CongViec = cboCongViec.SelectedValue.ToString();
                    cls.ChucVu = cboChucVu.SelectedValue.ToString();
                    if (txtLuongCoBan.Text == "")
                    {
                        MessageBox.Show("Chưa nhập lương cơ bản !");
                    }
                    else
                    {
                        Decimal temp = Convert.ToDecimal(txtLuongCoBan.Text);
                        txtLuongCoBan.Text = temp.ToString("#,###");
                        cls.LuongCB = Decimal.Parse(txtLuongCoBan.Text);

                        cls.Height = 0;
                        cls.Weight = 0;
                        if (txtHeSoLuong.Text == "")
                        {
                            MessageBox.Show("Chưa nhập hệ số lương !");
                        }
                        else
                        {
                            cls.HeSoLuong = int.Parse(txtHeSoLuong.Text);

                            cls.SoLaoDong = txtSoLaoDong.Text;
                            cls.NgayCapSLD = dNgayCapSLD.DateTime;
                            cls.NoiCapSLD = txtNoiCapSLD.Text;
                            cls.BankAcount = txtSoNganHang.Text;
                            cls.Bank = txtTenNganHang.Text;
                            cls.Health = "";
                            cls.Weight = 0;
                            cls.Height = 0;
                            cls.SoTheDoan = txtSoTheDoan.Text;
                            cls.NgayCapSTD = dNgayCapSTDoan.DateTime;
                            cls.NoiCapSTD = txtNoiCapSTDoan.Text;
                            cls.SoTheDang = txtSoTheDang.Text;
                            cls.NgayCapSTDang = dNgayCapSTDang.DateTime;
                            cls.NoiCapSTDang = txtNoiCapSTDang.Text;
                            cls.SoBaoHiemXH = txtBHXH.Text;
                            cls.NgayCapSBHXH = dNgayCapBHXH.DateTime;
                            cls.NoiCapSBHXH = dNgayCapBHYT.Text;
                            cls.SoBaoHiemYT = txtSoBHYT.Text;
                            cls.NgayCapSBHYT = dNgayCapBHYT.DateTime;
                            cls.NoiCapSBHYT = txtNoiCapBHYT.Text;
                            cls.SoHoChieu = txtSoHC.Text;
                            cls.NgayBDSHC = dNgayBDHC.DateTime;
                            cls.NgayKTSHC = dNgayKTHC.DateTime;
                            cls.NoiCapSHC = txtNoiCapSHC.Text;
                            cls.NgayVaoLam = dNgayVaoLam.DateTime;
                            cls.GhiChu = rtxtGhiChu.Text;
                            byte[] picbyte;
                            //if (HinhNV.ImageLocation == "")
                            if (HinhNV.ImageLocation == null)
                            {
                                MemoryStream ms = new MemoryStream();
                                Bitmap bit = new Bitmap(prjQuanLyNhanSu.Properties.Resources.HinhMacDinh);
                                bit.Save(ms, System.Drawing.Imaging.ImageFormat.Jpeg);
                                picbyte = new Byte[ms.Length];
                                ms.Position = 0;
                                ms.Read(picbyte, 0, Convert.ToInt32(ms.Length));//dua vao vi tri
                                cls.Image = picbyte;
                            }
                            else
                            {
                                FileStream frm;
                                frm = new FileStream(HinhNV.ImageLocation, FileMode.Open, FileAccess.Read);
                                picbyte = new byte[frm.Length];
                                frm.Read(picbyte, 0, System.Convert.ToInt32(frm.Length));
                                frm.Close();
                                cls.Image = picbyte;
                            }
                            if (txtSoNgayPhep.Text == "")
                            {
                                MessageBox.Show("Chưa nhập số ngày phép !");
                            }
                            else
                            {
                                cls.SoNgayPhep = int.Parse(txtSoNgayPhep.Text);

                                if (Sua == true)
                                {
                                    cls.Update();
                                    LoadDuLieu();
                                    //frmLuoi.Hide();
                                    frmLuoi.LoadDuLieu();
                                    MessageBox.Show("Sửa thành công !");
                                    //frmLuoi.ShowDialog();
                                }
                                else
                                {
                                    cls.Insert();
                                    LoadDuLieu();
                                    frmLuoi.LoadDuLieu();
                                    MessageBox.Show("Thêm thành công !");
                                }
                            }
                        }
                    }
                }
            }
        }
    }
}
    

